package com.mzpai.product.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImageMergeUtil {
	
	static Logger log = LoggerFactory.getLogger(ImageMergeUtil.class);

    //拼接太阳码和公司logo方法
    //传入参数：unCodeUrl 太阳码路径，logoUrl 公司logo路径
    //返回参数BufferedImage 对象
    public static BufferedImage replaceSunCodeLogo (String sunCodeUrl,String logoUrl){
        //通过太阳码路径获得logo图片对象
        BufferedImage sunImage = ImageGenerateUtil.getUrlByBufferedImage(sunCodeUrl);
        //通过logo路径获得logo图片对象
        BufferedImage logoImage = ImageGenerateUtil.getUrlByBufferedImage(logoUrl);
        //调用剪切图片方法将图片剪成正方形，
        //后面两个参数是图片的高，或者是图片的宽，要一样，方法会自动判断图片是宽大于高还是高大于宽
        BufferedImage squareImage = resize(logoImage,logoImage.getWidth(),logoImage.getWidth());
        //将正方形logo图片变成圆形
        BufferedImage avatarLogoImage = copeImage(squareImage);
        //调用覆盖方法，返回组装好的图片对象。第一个参数太阳码图片对象，第二个参数logo图片对象
        BufferedImage newsunImage = mergeImage(sunImage,avatarLogoImage);
        if(newsunImage == null){
            log.info("拼接后图片对象为空");
        }
        return newsunImage;
    }

    //拼接太阳码和公司logo方法,形式参数不同
    //传入参数：sunCodeUrlImage 太阳码图片BufferedImage 对象，logoUrl 公司logo路径
    //返回参数BufferedImage 对象
    public static BufferedImage replaceSunCodeLogo (BufferedImage sunCodeUrlImage,String logoUrl){
        //通过太阳码路径获得logo图片对象
        BufferedImage sunImage = sunCodeUrlImage;
        //通过logo路径获得logo图片对象
        BufferedImage logoImage = ImageGenerateUtil.getUrlByBufferedImage(logoUrl);
        //调用剪切图片方法将图片剪成正方形，
        //后面两个参数是图片的高，或者是图片的宽，要一样，方法会自动判断图片是宽大于高还是高大于宽
        BufferedImage squareImage = resize(logoImage,logoImage.getWidth(),logoImage.getWidth());
        //将正方形logo图片变成圆形
        BufferedImage avatarLogoImage = copeImage(squareImage);
        //调用覆盖方法，返回组装好的图片对象。第一个参数太阳码图片对象，第二个参数logo图片对象
        BufferedImage newsunImage = mergeImage(sunImage,avatarLogoImage);
        if(newsunImage == null){
            log.info("拼接后图片对象为空");
        }
        return newsunImage;
    }

    //sunImage 太阳码图片对象
    //logoImage 公司logo图片对象
    //这个方法是把公司logo盖在太阳码上面
    public static BufferedImage mergeImage(BufferedImage sunImage, BufferedImage logoImage) {
        //创建Graphics2D绘图
        Graphics2D graphics = sunImage.createGraphics();
        // 设置抗锯齿的属性
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        int qrWidth = sunImage.getWidth();// 二维码宽度
        int qrHeight = sunImage.getHeight();// 二维码高度
        int logoQrWidth = (int) (qrWidth / 2.2); // 组合之后logo的宽度(占二维码的1/2.2)
        int logoQrHeight = (int) (qrHeight / 2.2);// 组合之后logo的高度
        // 左边距、上边距、宽度、高度。覆盖原始图片中间的logo
        graphics.drawImage(
                logoImage,
                (qrWidth - logoQrWidth) / 2,
                (qrHeight - logoQrHeight) / 2,
                logoQrWidth,
                logoQrHeight,
                null);
        //关闭
        graphics.dispose();
        return sunImage;
    }


    //将logo变成圆形,记得给个正方形的图片，否则会不是正规圆形
    public static BufferedImage copeImage(BufferedImage avatarImage) {
        try {
            int width = avatarImage.getWidth();
            int height = avatarImage.getHeight();
            // 透明底的图片
            BufferedImage formatAvatarImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D graphics = formatAvatarImage.createGraphics();
            // 把图片切成一个圆
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            // 留一个像素的空白区域，这个很重要，画圆的时候把这个覆盖
            int border = 1;
            // 图片是一个圆型
            Ellipse2D.Double shape = new Ellipse2D.Double(border, border, width - border * 2, width - border * 2);
            // 需要保留的区域
            graphics.setClip(shape);
            graphics.drawImage(avatarImage, border, border, width - border * 2, width - border * 2, null);
            graphics.dispose();
            // 在圆图外面再画一个圆
            // 新创建一个graphics，这样画的圆不会有锯齿
            graphics = formatAvatarImage.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            int border1 = 3;
            //画笔是4.5个像素，BasicStroke的使用可以查看下面的参考文档
            //使画笔时基本会像外延伸一定像素，具体可以自己使用的时候测试
            Stroke s = new BasicStroke(5F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
            graphics.setStroke(s);
            graphics.setColor(Color.WHITE);
            graphics.drawOval(border1, border1, width - border1 * 2, width - border1 * 2);
            graphics.dispose();
            return formatAvatarImage;
        } catch (Exception e) {
//            log.info("将头像变为圆角异常",e);
            e.printStackTrace();
        }
        return null;
    }

    //此方法将矩形图片裁剪成想要的样式，
    //param: needW 想要的宽度
    //param: needH 想要的高度
    //把长方形的图片剪成正方形
    public static BufferedImage resize(BufferedImage image, int needW, int needH) {
        int resizeW, resizeH, resizeX, resizeY;
        int imgW = image.getWidth(), imgH = image.getHeight();
        int wantH = imgW * needH / needW;
        if (wantH < imgH) {
            resizeH = imgW * needH / needW;
            resizeW = imgW;
            resizeX = 0;
            resizeY = (imgH - wantH) / 2;
        }
        else {
            resizeH = imgH;
            resizeW = imgH * needW / needH;
            resizeX = (imgW - imgH * needW / needH) / 2;
            resizeY = 0;
        }
        BufferedImage image1 = image.getSubimage(resizeX, resizeY, resizeW, resizeH);
        return image1;
    }
}
